﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace CoreQ
{
    public class Core
    {
        #region AdminSystem
        public class AdminSystem
        {
            private const string User = "_-_HeoISua_-_";
            private const string Password = "A1B2C3D4E5F6G7H8I9J0";

            /// <summary>
            /// Author: QuyBX
            /// CreatedDate: 20/09/2014
            /// Kiểm tra quyền admin
            /// </summary>
            /// <param name="user"></param>
            /// <param name="pass"></param>
            /// <returns></returns>
            public static bool CheckAdminSystem(string user, string pass)
            {
                bool IsAdmin = false;
                if (AdminSystem.User == user && AdminSystem.Password == pass)
                    IsAdmin = true;
                return IsAdmin;
            }
        }
        #endregion

        #region Cookie
        public static string GetCookie(string CookieName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[CookieName];
            return cookie != null && !string.IsNullOrEmpty(cookie.Value) ? Base64Decode(cookie.Value) : "";
        }

        public static void SetCookie(string CookieName, string CookieValue)
        {
            SetCookie(CookieName, CookieValue, 10);
        }

        public static void SetCookie(string CookieName, string CookieValue, int Date)
        {
            HttpCookie cookie = new HttpCookie(CookieName);
            cookie.Value = Base64Encode(CookieValue);
            cookie.Expires = DateTime.Now.AddDays(Date);
            cookie.Path = "/";
            cookie.HttpOnly = true;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        #endregion

        #region Encode Base64
        public static string Base64Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        #endregion

        #region Decode Base64
        public static string Base64Decode(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }
        #endregion

        static string EncryptionKey = "!5623a#de";
        #region Encode with EncrytionKey
        public static string EncodeEK(string src)
        {
            byte[] bytes;
            try
            {
                bytes = Encoding.Unicode.GetBytes(EncryptionKey + src);
            }
            catch
            {
                return src;
            }
            return Convert.ToBase64String(bytes);
        }
        #endregion

        #region Decode with EncrytionKey
        public static string DecodeEK(string src)
        {
            try
            {
                byte[] bytes = Convert.FromBase64String(src);
                src = Encoding.Unicode.GetString(bytes);
                src = src.Remove(0, 9);
            }
            catch
            {
                return src;
            }
            return src;
        }
        #endregion
    }
}